package com.zing.controller.admin;

import com.zing.dto.DishDTO;
import com.zing.dto.DishPageQueryDTO;
import com.zing.entity.Dish;
import com.zing.result.PageResult;
import com.zing.result.Result;
import com.zing.service.DishService;
import com.zing.vo.DishVO;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Set;

@RestController("adminDishController")
@RequestMapping("/admin/dish")
@Api(tags = "菜品相关接口")
@Slf4j
public class DishController {
    @Autowired
    private DishService dishService;
    @Autowired
    private RedisTemplate redisTemplate;

    //新增菜品
    @PostMapping
    @ApiOperation("新增菜品")
    public Result<String> addWithFlavor(@RequestBody DishDTO dishDTO){
        log.info("新增菜品, {}", dishDTO);
        dishService.addWithFlavor(dishDTO);

        // 清理接口:/user/dish/list的缓存
        Long categoryId = dishDTO.getCategoryId();
        String key = "dish_" + categoryId;
        cleanCache(key);

        return Result.success();
    }

    //分页查询
    @GetMapping("/page")
    @ApiOperation("分页查询菜品")
    public Result<PageResult> page(DishPageQueryDTO dishPageQueryDTO){
        log.info("分页查询菜品,{}", dishPageQueryDTO);
        PageResult pageResult = dishService.page(dishPageQueryDTO);
        return Result.success(pageResult);
    }
    
    //删除菜品 @RequestParam 能将请求参数中的ids="id1,id2,id3"强转为为List
    @DeleteMapping
    @ApiOperation("批量删除菜品")
    public Result<String> delete(@RequestParam List<Long> ids){
        log.info("批量删除菜品: {}", ids);
        dishService.delete(ids);
        cleanCache("dish_*"); // 清理接口:/user/dish/list的缓存
        return Result.success();
    }

    //根据id,查找菜品信息
    @GetMapping("/{id}")
    @ApiOperation("根据菜品id,查找菜品信息")
    public Result<DishVO> getById(@PathVariable Long id){
        log.info("根据id,查找菜品信息, {}", id);
        return Result.success(dishService.getByIdWithFlavor(id));
    }

    //修改菜品信息
    @PutMapping
    @ApiOperation("修改菜品信息")
    public Result<String> updateWithFlavor(@RequestBody DishDTO dishDTO){
        log.info("修改菜品信息, {}", dishDTO);
        dishService.updateWithFlavor(dishDTO);
        cleanCache("dish_*"); // 清理接口:/user/dish/list的缓存
        return Result.success();
    }

    // 菜品起售停售
    @PostMapping("/status/{status}")
    @ApiOperation("菜品起售停售")
    public Result<String> startOrStop(@PathVariable Integer status, Long id) {
        dishService.startOrStop(status, id);

        cleanCache("dish_*"); // 清理接口:/user/dish/list的缓存
        return Result.success();
    }

    // 根据分类id查询菜品
    @GetMapping("/list")
    @ApiOperation("根据分类id查询菜品")
    public Result<List<Dish>> list(Long categoryId){
        List<Dish> list = dishService.list(categoryId);
        return Result.success(list);
    }

    // 清理缓存数据
    private void cleanCache(String regexpKey) {
        Set keys = redisTemplate.keys(regexpKey);
        redisTemplate.delete(keys);
    }
}
